/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package file;

import com.sun.squawk.io.BufferedWriter;
import com.sun.squawk.microedition.io.FileConnection;
import edu.wpi.first.wpilibj.Timer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Vector;
import javax.microedition.io.Connector;

/**
 *
 * @author Conner Davis - Team 708
 */
public class oldDataRecorder {
    
    // Data that is recorded goes here

    // Autonomous mode number and 99 for teleop
    public int robotMode      = -1;               // done
    public String autoCommand = " ";              // done

    // operator inputs
    public double  driveStickX = 0;               // done
    public double  driveStickY = 0;               // done
    public double  driveStickZ = 0;               // done
    public double  magnitude   = 0;               // done
    public double  direction   = 0;               // done
    public double  operatorStickX = 0;            // currently not used
    public double  operatorStickY = 0;            // currently not used
    public double  operatorStickZ = 0;            // currently not used
    public boolean driveControlButton = false;    // done
    public boolean minibotDeployButton = false;   // done

    // sensors
    public double elevator       = 0.0;
    public double gripper        = 0.0;
    public double wrist          = 0.0;
    public long   frontLeftDist  = 0;             // done
    public long   frontRightDist = 0;             // done
    public long   rearLeftDist   = 0;             // done
    public long   rearRightDist  = 0;             // done

    public int    lineSensors    = 0;             // done

    public double gyroHeading = 0;                // done

    // actuators


    // To record data
    private Vector data = new Vector(1000);
    private FileConnection fcTelemetry;
    private OutputStream ost;
    private BufferedWriter ostream;
    private Timer timer;

    private static oldDataRecorder m_instance = null;

    public oldDataRecorder()
    {
        timer= new Timer();
        printHeader();
    }

    public void checkForFile()
    {
        try
        {
            fcTelemetry = (FileConnection) Connector.open("file:////ni-rt/startup/telemetry data.csv",
                                                              Connector.READ_WRITE);
            if (fcTelemetry.exists()) {
                fcTelemetry.delete();
            }
            fcTelemetry.create();
            ost = fcTelemetry.openOutputStream();
            ostream = new BufferedWriter(new OutputStreamWriter(ost));
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }

    public static oldDataRecorder getInstance() {
        if (m_instance == null) {
            m_instance = new oldDataRecorder();
        }
        return m_instance;
    }

    public void writeToFile()
    {
        int index = 0;
        boolean flag = false; // changes to true when there are no more elements
        try
        {
            while (flag == false)
            {
                try
                {
                    ostream.write((String) data.elementAt(index));
                    ostream.newLine();
                    index++;
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    flag = true;
                }
            }
        }
        catch (IOException e)
            {
                System.out.println("Data Recorder: Write to file: "+e.getMessage());
            }
        try
        {
            ostream.close();
            timer.stop();
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }

    private void printHeader()
    {
        // modify the header row if fields below are added/deleted
        data.addElement("Time,AutoMode,AutoCommand," +
                        "FLdist,FRdist,RLdist,RRdist,LineSensors," +
                        "Elev,Gripper,Wrist,Gyro");
    }

    public void log()
    {
         data.addElement(timer.get()        + "," +  // subset of complete list below
                         robotMode          + "," +
                         autoCommand        + "," +
                         frontLeftDist      + "," +
                         frontRightDist     + "," +
                         rearLeftDist       + "," +
                         rearRightDist      + "," +
                         lineSensors        + "," +
                         elevator           + "," +
                         gripper            + "," +
                         wrist              + "," +
                         gyroHeading
                         );
/*         data.addElement(timer.get()        + "," +
                        robotMode          + "," +
                        autoCommand        + "," +
                        kickerState        + "," +
                        driveStickX        + "," +
                        driveStickY        + "," +
                        driveStickZ        + "," +
                        magnitude          + "," +
                        direction          + "," +
                        operatorStickX     + "," +
                        operatorStickY     + "," +
                        operatorStickZ     + "," +
                        frontKickButton    + "," +
                        sideKickButton     + "," +
                        driveControlButton + "," +
                        releaseArmButton   + "," +
                        frontLeftDist      + "," +
                        frontRightDist     + "," +
                        rearLeftDist       + "," +
                        rearRightDist      + "," +
                        frontBallDetected  + "," +
                        sideBallDetected   + "," +
                        gyroHeading        + "," +
                        frontSmallPiston   + "," +
                        frontLargePiston   + "," +
                        sideSmallPiston    + "," +
                        sideLargePiston    + "," +
                        frontKickerReady   + "," +
                        sideKickerReady    + "," +
                        grapplerArmPos );
 *
 */
    }

    public void startTimer()
    {
        timer.stop();
        timer.reset();
        timer.start();
    }
}